package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.User;
import com.jt.service.DubboUserService;
import com.jt.service.UserService;
import com.jt.util.CookieUtil;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.JedisCluster;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller //涉及到页面跳转
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private UserService userService;

    @Reference(check = false, timeout = 3000)
    private DubboUserService dubboUserService;

    @Autowired
    private JedisCluster jedisCluster;

    /**
     * 通用页面跳转功能
     * 1. http://www.jt.com/user/login.html     login.jsp页面
     * 2. http://www.jt.com/user/register.html  register.jsp页面
     */

    @RequestMapping("{moduleName}")
    public String module(@PathVariable String moduleName) {
        return moduleName;
    }

    /**
     * 测试httpClient
     */
    @RequestMapping("findUserById/{userId}")
    @ResponseBody
    public User findUserById(@PathVariable Long userId) {

        return userService.findUserById(userId);
    }

    @RequestMapping("doRegister")
    @ResponseBody
    public SysResult doRegister(User user) {
        dubboUserService.saveObject(user);
        return SysResult.success();
    }

    /**
     * 完成用户登录
     * url : http://www.jt.com/user/doLogin?r=0.9588654050157963
     * 参数: username password
     * 返回值 SysResult对象 的json数据
     * <p>
     * cookie.setMaxAge(0) 立即删除cookie
     * cookie.setMaxAge(-1) 关闭浏览器会话时删除
     * cookie.setMaxAge(100) cookie存活时间,单位是秒
     */
    @RequestMapping("doLogin")
    @ResponseBody
    public SysResult doLogin(User user, HttpServletResponse response) {
        //1.实现用户的登陆操作
        String ticket = dubboUserService.doLogin(user);

        //2.校验ticket是否有值
        if (StringUtils.isEmpty(ticket)) {
            return SysResult.fail();
        }
        //3.如果用户的ticket不为null,则表示登陆正确,需要将数据保存到cookie中
        //cookie要求  7天有效   cookie可以在jt.com的域名中共享   cookie权限 /
        Cookie cookie = new Cookie("JT_TICKET", ticket);
        cookie.setMaxAge(60 * 60 * 24 * 7); //cookie存活7天
        cookie.setDomain("jt.com");
        cookie.setPath("/");
        response.addCookie(cookie);

        return SysResult.success();
    }

    /**
     * 登出操作 删除cookie 清空redis数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        String ticket = CookieUtil.getCookieValue("JT_TICKET", request);

        if (!StringUtils.isEmpty(ticket)) {
            //删除redis
            jedisCluster.del(ticket);
            //删除cookie
            CookieUtil.deleteCookie("JT_TICKET", "/", "jt.com", response);
        }

        return "redirect:/";

        //删除cookie
//        Cookie[] cookies = request.getCookies();
//        if(cookies != null && cookies.length >0){
//            for (Cookie cookie : cookies) {
//                if ("JT_TICKET".equals(cookie.getName())) {
//                    String value = cookie.getValue();
//                    jedisCluster.del(value); //清空redis
//                    //删除cookie时,必须与原来的cookie数据保持一致
//                    cookie.setDomain("jt.com");
//                    cookie.setPath("/");
//                    cookie.setMaxAge(0);
//                    response.addCookie(cookie);
//                    break;
//
//                }
//            }
//        }
    }
}
